package tw.com.msig.spring.hibernate.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tw.com.msig.spring.hibernate.entity.Role;

@Repository // <bean id="userDao" class="tw.com.msig.spring.hibernate.dao.UserDao"/>
public class RoleDao {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public List<Role> findAll() {
		Session session = sessionFactory.getCurrentSession();
		
		return session.createCriteria(Role.class)
				.addOrder(Order.asc("id"))
				.list();
		//return session.createQuery("from tw.com.msig.spring.hibernate.entity.Role").list();
		
		
	}

	
	public Role insert(Role Role) {
		Session session = sessionFactory.getCurrentSession();
		session.save(Role);
		return Role;
	}
	
	public Role findOne(Long id) {
		Session session = sessionFactory.getCurrentSession();
		return (Role)session.createCriteria(Role.class)
				.add(Restrictions.eq("id", id))
				.uniqueResult();
	}
	
	
	public void delete(Long id) {
		Session session = sessionFactory.getCurrentSession();
		session.delete(findOne(id));
		
	}
	
	public void update(Role Role) {
		Session session = sessionFactory.getCurrentSession();
		session.update(Role);
	}
	
	
	
	
	
	
	
	
	
	

}
